package com.zhiyou100.controller;


import com.zhiyou100.pojo.User;
import com.zhiyou100.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.*;

@Controller
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService us;
    @RequestMapping("/list.action")
    public String list(@RequestParam(value = "page",defaultValue = "1") Integer page,
                       Model model){
        //pageSize自己定的  2
        //start  （page-1）*pageSize
        Integer pageSize=2;
        Integer start=(page-1)*pageSize;
        List<User> users=us.list(start,pageSize);
        //把list转换成map  map键唯一   键-账号
        Map<String,Set<User>> map=new HashMap<String,Set<User>>();
        //遍历users
        for (int i=0;i<users.size();i++){
            //获取用户
            User user=users.get(i);
            //获取账号
            String u_username=user.getU_username();
            //向map里面添加数据
            //第一次添加
            if (!map.containsKey(u_username)){
                Set<User> set=new HashSet<User>();
                set.add(user);
                map.put(u_username,set);
            }else {
                Set<User> set=map.get(u_username);
                set.add(user);
                map.put(u_username,set);
            }
        }
        model.addAttribute("map",map);
        //当前页
        model.addAttribute("page",page);
        //总页数
        long countPage=us.countPage(pageSize);
        model.addAttribute("countPage",countPage);
        //总条数
        long count=us.findAllCount();
        model.addAttribute("count",count);
        return "User/index";
    }

}
